Let’s fetch some data:
from pandas.io import wb
eu_countries = ['BE', 'BG', 'CZ', 'DK', 'DE', 'EE', 'IE', 'GR', 'ES', 'FR', 'HR',
'IT', 'CY', 'LV', 'LT', 'LU', 'HU', 'MT', 'NL', 'AT', 'PL', 'PT',
'RO', 'SI', 'SK', 'FI', 'SE', 'GB']
ue = wb.download(indicator = "SL.UEM.TOTL.ZS",
country = eu_countries, start = 1991,
end = 2013)
ue.reset_index(inplace = True)
ue.columns = ['country', 'year', 'unemployment']seabornseaborn’s heatmap() function; takes a wide data frame with x-values in the index and y-values as column headersue_wide = ue.pivot(index = 'country', columns = 'year',
values = 'unemployment')
sns.heatmap(ue_wide)seaborn ‘heat map’seabornseabornseabornColor palettes, available in the color_palette() function, can be viewed with the palplot() function
sns.palplot(sns.color_palette('Greens', 7))Colors can be reversed by adding _r:
sns.palplot(sns.color_palette('Greens_r', 7))seaborncolor_palette() also allows for the creation of custom palettes!colors = ['#F5A422', '#3E22F5', '#3BF522',
'#C722F5', '#F53E22']
pal = sns.color_palette(colors)
sns.palplot(pal)seabornmx = pd.read_csv('http://personal.tcu.edu/kylewalker/mexico.csv')
sns.barplot(x = 'gdp08', y = 'name', data = mx.sort('gdp08', ascending = False), palette = "Greens_r")sns.set_style('white')
full = ue.pivot(index = 'year', columns = 'country', values = 'unemployment')
greece = full['Greece']
full.plot(legend = False, style = 'lightgrey')
greece.plot(style = 'blue', legend = True)plt.annotate('Global recession \nspreads to Europe', xy = (2009, 9.5),
xycoords = 'data', xytext = (2005, 15), textcoords = 'data',
arrowprops = dict(arrowstyle = 'simple', color = '#000000'))pandasfull.plot(subplots = True, layout = (7, 4), figsize = (12, 10), sharey = True )seabornseaborn can be “faceted” with factorplot or mapped onto a faceted plot with FacetGrid.plot() in pandas and seaborn are wrappers around matplotlib, the main plotting engine for Pythonmatplotlib customization methods are available for your pandas and seaborn plots - and there are many!import matplotlib.pyplot as pltimport matplotlib.pyplot as plt
plt.figure(figsize = (10, 7))
sns.heatmap(ue_wide, cmap = 'YlGnBu')
plt.ylabel("")
plt.xlabel("")
plt.title("Unemployment in Europe, 1991-2013")
plt.xticks(rotation = 45)seaborn and matplotlibseaborn returns a matplotlib object that can be modified by the options in the pyplot moduleseaborn and .plot() in pandas and available as arguments - so check the documentation to see what you can do!import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set_style('white')
ue['year2'] = ue.year.astype(float)
full = ue.pivot(index = 'year2', columns = 'country',
values = 'unemployment')
greece = full['Greece']
full.plot(style = 'lightgrey', legend = False, figsize = (10, 7))
greece.plot(style = 'blue', legend = True)
plt.xlabel("")
plt.ylabel("Unemployment rate")
plt.annotate('Global recession \nspreads to Europe', xy = (2009, 9.5),
xycoords = 'data', xytext = (2005, 15), textcoords = 'data',
arrowprops = dict(arrowstyle = 'simple', color = '#000000'))
plt.yticks(range(0, 31, 5), [str(x) + '%' for x in range(0, 31, 5)])mx.plot(x = 'mus09', y = 'gdp08', kind = 'scatter', logy = True)sns.lmplot(data = mx, x = 'mus09', y = 'pri10', lowess = True)sns.pairplot(data = mx, vars = ['gdp08', 'mus09', 'pri10'])plt.savefig('destfile.jpg', dpi = XXX)